package edu.cs346.group5.ui.loginpages;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JTextField;
import javax.swing.JPasswordField;
import net.miginfocom.swing.MigLayout;
import javax.swing.JSpinner;
import javax.swing.JComboBox;

import edu.cs346.group5.backend.Database;
import edu.cs346.group5.datastores.Item;
import edu.cs346.group5.datastores.User;
import edu.cs346.group5.logic.Login;

public class register extends JFrame {

	private JPanel contentPane;
	private JTextField txtFirstName;
	private JTextField txtLastName;
	private JTextField txtEmail;
	private JPasswordField passwordField;
	private JButton btnRegister;
	private JButton btnBack;
	private JTextField textField_1;
	private JComboBox comboBox;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public register() {
		setTitle("Register");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[86px,grow][89px][]",
				"[20px][20px][][][20px][][][][][][][20px][][][][][23px]"));

		txtFirstName = new JTextField();
		txtFirstName.setText("First Name");
		contentPane.add(txtFirstName, "cell 0 0 3 1,growx,aligny top");
		txtFirstName.setColumns(10);

		txtLastName = new JTextField();
		txtLastName.setText("Last Name");
		contentPane.add(txtLastName, "cell 0 1 3 1,growx,aligny top");
		txtLastName.setColumns(10);

		txtEmail = new JTextField();
		txtEmail.setText("email");
		contentPane.add(txtEmail, "cell 0 2 3 1,growx");
		txtEmail.setColumns(10);

		btnRegister = new JButton("Register");
		btnRegister.addMouseListener(new MouseAdapter() {
			// TODO add user to database
			public void mousePressed(MouseEvent e) {
				User user = new User();
				user.FirstName = txtFirstName.getText();
				user.LastName = txtLastName.getText();
				user.Email = txtEmail.getText();

				if (validateEmail(user.Email)) {
					Item item = (Item) comboBox.getSelectedItem();
					Login.RegisterUser(user, item.getId(),
							textField_1.getText(), passwordField.getText());
					// go back to login page
					login x = new login();
					x.setVisible(true);
					dispose();
				}
				else
				{
					System.out.println(user.Email+ " is not a valid email.");
					register x = new register();
					x.setVisible(true);
					dispose();
				}

				
			}
		});

		// Get security questions
		List<HashMap<String, Object>> results = null;
		try {
			results = Database.select("select * from SecretQuestions");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		comboBox = new JComboBox();
		// fill the list
		for (int i = 0; i < results.size(); i++) {
			comboBox.addItem(new Item((int) results.get(i).get("id"), results
					.get(i).get("question").toString()));
		}

		passwordField = new JPasswordField("password");
		contentPane.add(passwordField, "cell 0 3 3 1,growx,aligny top");

		contentPane.add(comboBox, "cell 0 7 3 1,growx");

		textField_1 = new JTextField("Answer");
		contentPane.add(textField_1, "cell 0 8 3 1,growx");
		textField_1.setColumns(10);
		contentPane.add(btnRegister, "cell 2 9,growx");

		btnBack = new JButton("Back");
		btnBack.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				login x = new login();
				x.setVisible(true);
				dispose();
			}
		});
		contentPane.add(btnBack, "cell 2 10,growx");
	}

	public boolean validateEmail(String str) {

		String regEx = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);

		if (m.find()) {
			return true;
		}
		return false;
	}

}
